app.controller('riskManagementCtrl',['$scope','$rootScope','$http','cookies','myHttp','pcdService','$filter','listSve',function($scope,$rootScope,$http,cookies,myHttp,pcdService,$filter,listSve){
	
	//$rootScope.userName = cookies.getCookie('userName'); //获取cookie用户名
	$rootScope.navIndex = 3;
	
	angular.extend($scope,{
		
		//初始化
		init: function(){
			$scope._roomName = '';  //搜索用户名字
			$scope._roomPhone = '';  //搜索用户电话
			$scope.listData = {};  //页面列表数据
			$scope.iMax = 20;
			$scope.iMax2 =20;
			$scope.isFirst = true;
			
		    //获取省列表
		   	pcdService.prolist().list().then(function(e) {
	            $scope.prolist = e.data;
	        })
	        pcdService.citylist().list().then(function(e) {
	        	 $scope.oCityList = e.data;
	        })
	        pcdService.dislist().list().then(function(e) {
	        	 $scope.oDisList = e.data;
	        })
		    
		},
		
		//搜索
		search: function(){
			
			if($scope._roomPhone == ''){
				$scope.alert({show: true, Title:'系统提示', Message:'手机号不能为空', prevId:'',Type:false});
			} else{
				
				var re = /^1[3|4|5|7|8][0-9]{9}$/;
				
				if(re.test($scope._roomPhone)){
					
					$scope.isFirst = true;
					
					listSve.riskManagementList().list({mobile: $scope._roomPhone,numPerPage:20}).then(function(data) {
				
						console.log(data)
						$scope.listData = data;
						if(data.res.data == 0){
							$scope.alert({show: true, Title:'系统提示', Message:'该号码没有数据', prevId:'',Type:false});
						} else{
							//创建分页
					        $scope.iMax = Math.ceil(data.res.page.recordCount/20);
							$scope.doPager('pager_1',$scope.iMax,$scope.doChangePage);
						}
						
						
						
			        });
					
				} else{
					
					$scope.alert({show: true, Title:'系统提示', Message:'请输入正确手机号', prevId:'',Type:false});
					
				}
				
				
			}
			
			
		},
		
		//生成分页
		doPager: function(box,maxNum,pageFn){
			var pagerBox = document.getElementById(box);
				pagerBox.innerHTML = '';
			var pager = new Pager({
					index: 1,
					total: maxNum,
					parent: pagerBox,
					onchange: pageFn
				});
		},
		
		//开门记录分页执行事件
		doChangePage2: function(obj){
			
			if($scope.isFirst){
				
				$scope.isFirst = false;
				
			} else{
				
				//请求
				$scope.arges = {
					url: '',
					method: 'POST',
					params: {
						
					}
				}
				myHttp.loadData($scope.arges).then(function(data) {  
					
					console.log(data)
					$scope.doorList = data;
					
				},function(data){  
					
					alert("请求失败");
					
				}) 
				
			}
			
		},
		
		//设备状态
		status: function(x,n){
			
			for(var i=0;i<x.hardwares.length;i++){
				
				if(x.hardwares[i].type == n){
					
					var className = '';
					var s = x.hardwares[i].hardwareStatus;
					
					if(s == 'ZC'){
						
						return className += 'ZC';
						
					} else if(s == 'YC'){
						
						return className += 'YC';
						
					} else if(s == 'WZC'){
						
						return className += 'WZC';
						
					} else if(s == 'SD'){
						
						return className += 'SD';
						
					}
					
					return className;
					
				};
				
			}
			
		}
		
	}).init();
	
	//电量历史
	$scope.power = function(e,d){
		$scope.chart = true;
		document.getElementById('chart').innerHTML = '';
		document.getElementById('chart_1').innerHTML = '';
		//powerList
		/*$scope.power = {};
		$scope.power.devId = e;
		$scope.power.type = 'week';*/
		$scope.powerObj = {
			devId:e,
			type:'week'
		}
		listSve.powerList().list($scope.powerObj).then(function(e) {
			var txt = d;
			console.log(e);
			var dataName = e.res.data.data;
			var oDataName = [];
			var oData = [];
			for (var i=0;i<dataName.length;i++) {
				oDataName.unshift(dataName[i].Date)
//				oData.unshift(dataName[i].Allpower)
				oData.unshift(Math.floor((dataName[i].Allpower) * 100) / 100)  
			}
			console.info(oDataName)
			// 基于准备好的dom，初始化echarts实例
        	var myChart = echarts.init(document.getElementById('chart'));
       		 // 指定图表的配置项和数据
	        var option = {
	            title: {
	                text: txt,
	                subtext: '单位 ：度'
	            },
	            tooltip: {
	            	trigger: 'axis'
	            },
	            xAxis: {
	                data: oDataName
	            },
	            yAxis: {},
	            series: [{
	                name: '电量（度）',
	                type: 'line',
	                data: oData
	            }]
	        };
        	// 使用刚指定的配置项和数据显示图表。
       		myChart.setOption(option);
		})
		
		//月用电量数据---------------------------------------//		
		$scope.power_1 = {};
		$scope.power_1.devId = e;
		$scope.power_1.type = 'month';
		listSve.powerList().list($scope.power_1).then(function(e) {
			var txt = d;
			console.log(e);
			var dataName = e.res.data.data;
			var oDataName = [];
			var oData = [];
			for (var i=0;i<dataName.length;i++) {
				oDataName.unshift(dataName[i].Date)
//				oData.unshift(dataName[i].Allpower)
				oData.unshift(Math.floor((dataName[i].Allpower) * 100) / 100)  
			}
			console.info(oDataName)
			console.log(oData)
			// 基于准备好的dom，初始化echarts实例
       		 var myChart = echarts.init(document.getElementById('chart_1'));
        	// 指定图表的配置项和数据
	        var option = {
	            title: {
	                text: txt,
	                subtext: '单位 ：度'
	            },
	            tooltip: {
	            	trigger: 'axis'
	            },
	            xAxis: {
	                data: oDataName
	            },
	            yAxis: {},
	            series: [{
	                name: '电量（度）',
	                type: 'line',
	                data: oData
	            }]
	        };
        	// 使用刚指定的配置项和数据显示图表。
        	myChart.setOption(option);
		})
	}
	
	$scope.doorModal = function(o,h){
		if (o.type == "DIANBIAO") {
			$scope.mdl = '#modal_5';
			$scope.warsName = '电表';
			$scope.supname = o.ammeter.supplier.name;
			$scope.devId = o.ammeter.devId;
			//$scope.hardwdress =$filter('proName')(h.houseStreet.provinceId,$scope.prolist)+$filter('cityName')(h.houseStreet.cityId,$scope.oCityList)+$filter('disName')(h.houseStreet.dist,$scope.oDisList)+h.houseStreet.street;
			$scope.hardwdress = $filter('proName')(h.houseStreet.provinceId, $scope.prolist) + $filter('cityName')(h.houseStreet.cityId, $scope.oCityList) + $filter('disName')(h.houseStreet.dist, $scope.oDisList) + h.houseStreet.street + h.floorNo + "栋" + h.unit + "单元" + h.roomNo + "室" + h.door;
			$scope.payMod = o.ammeter.payMod;
			$scope.install = o.ammeter.installTime
			$scope.electricityPrice = o.ammeter.electricityPrice;
			$scope.status = {};
			$scope.status.devId =o.ammeter.devId;
			listSve.zStatus().list($scope.status).then(function(e) {
				console.log(e);
				if (e.code == 1) {
					$scope.alert({show: true, Title:'系统提示', Message:e.res.msg,prevId:'',Type:false});
				} else{
					$('#modal_5').modal('show')
					var oId = o.type+h.id;
					angular.element("#"+oId).attr({'class':e.res.data.hardwareStatus});
					$scope.hardwareStatus_1 = e.res.data.hardwareStatus;
					$scope.payMod = e.res.data.mode;;
					$scope.electricityPrice = e.res.data.price;;
					$scope.hardwareStatus_2 = e.res.data.td_status;
					if($scope.hardwareStatus_1 == 'ZC'){
						$scope.hardwareStatus_1 = '在线';
						$scope.clor = "";
					} else{
						$scope.clor = "red";
						$scope.hardwareStatus_1 = '离线';
					};
					if($scope.hardwareStatus_2 == 0){
						$scope.clor_t = "red";
					}else{
						$scope.clor_t = "";
					};
				}
				
				/*	console.log(e)
				$scope.hardwareStatus_1 = e.res.data.line_status;
				$scope.hardwareStatus_2 = e.res.data.td_status;
				if($scope.hardwareStatus_1 == 1){
					$scope.clor = "red";
				};
				if($scope.hardwareStatus_2 == 0){
					$scope.clor_t = "red";
				};*/
				
			})
			
			
			
			
			
		}else if(o.type == "MENSUO"){
			$scope.mdl = '#modal_4';
			$scope.warsName = '门锁';
			$scope.supname_1 = o.doorLock.supplier.name;
			$scope.devId_1 = o.doorLock.devId;
			$filter('proName')(h.houseStreet.provinceId,$scope.prolist)+$filter('cityName')(h.houseStreet.cityId,$scope.oCityList)+$filter('disName')(h.houseStreet.dist,$scope.oDisList)
			//$scope.hardwdress_1 = $filter('proName')(h.houseStreet.provinceId,$scope.prolist)+$filter('cityName')(h.houseStreet.cityId,$scope.oCityList)+$filter('disName')(h.houseStreet.dist,$scope.oDisList)+h.houseStreet.street;
			//$scope.hardwdress_1_1 = h.districtName+$filter('proName')(h.houseStreet.provinceId,$scope.prolist)+$filter('cityName')(h.houseStreet.cityId,$scope.oCityList)+$filter('disName')(h.houseStreet.dist,$scope.oDisList)+h.houseStreet.street+ h.floorNo + '栋' + h.unit + "单元" + h.roomNo + "室" + h.door;
			$scope.hardwdress_1 = $filter('proName')(h.houseStreet.provinceId, $scope.prolist) + $filter('cityName')(h.houseStreet.cityId, $scope.oCityList) + $filter('disName')(h.houseStreet.dist, $scope.oDisList) + h.houseStreet.street + h.floorNo + '栋' + h.unit + "单元" + h.roomNo + "室" + h.door;
			$scope.hardwdress_1_1 = h.districtName + $filter('proName')(h.houseStreet.provinceId, $scope.prolist) + $filter('cityName')(h.houseStreet.cityId, $scope.oCityList) + $filter('disName')(h.houseStreet.dist, $scope.oDisList) + h.houseStreet.street + h.floorNo + '栋' + h.unit + "单元" + h.roomNo + "室" + h.door;

			
			//doorList
			$scope.door = {};
			$scope.door.devId =o.doorLock.devId;
			$scope.door.type = 'MENSUO';
			listSve.doorList().list($scope.door).then(function(e) {
				console.log(e)
				if (e.code == 1) {
//					$scope.alert = {Message:e.res.msg,Title:'系统提示',Type:false}
//					$('#ycf-alert').modal('show')
					$scope.alert({show: true, Title:'系统提示', Message:e.res.msg,prevId:'',Type:false});
				} else{
					$('#modal_4').modal('show')
					var oId_1 = o.type+h.id;
					$("#"+oId_1).attr({'class':e.res.data.hardwareStatus});
					$scope.hardwareStatus_4 = e.res.data.electric;
					$scope.hardwareStatus_3 = e.res.data.hardwareStatus;//在线
					
					if($scope.hardwareStatus_3 == 'ZC'){
						$scope.hardwareStatus_3 = '在线';
						$scope.clor_1 = "";
					} else{
						$scope.hardwareStatus_3 = '离线';
						$scope.clor_1 = "red";
					}
					
					if(e.res.data.electric<20){
						$scope.clor_2 = "red";
					}else{
						$scope.clor_2 = "";
					};
					/*if(e.res.data.success == "false"){
						$scope.clor_1 = "red";
					}*/
				}
				
				/*console.log(e);
				$scope.hardwareStatus_3 = e.res.data.hardwareStatus;
				if($scope.hardwareStatus_3 == 'ZC'){
					$scope.hardwareStatus_3 = '在线';
				} else if($scope.hardwareStatus_3 == 'YC'){
					$scope.hardwareStatus_3 = '离线';
				} else{
					$scope.hardwareStatus_3 = '离线';
				}
				
				
				if(e.code == '0'){
					$scope.hardwareStatus_4 = e.res.data.electric;
					if(e.res.data.electric<20){
						$scope.clor_2 = "red";
					}
				}*/
			})
			$scope.install_1 = o.doorLock.installTime
			
			if(o.hardwareStatus == "YC"){
				$scope.clor_1 = "red";
			}else{
				$scope.clor_1 = "";
			};
		}
		
		
	};
	//点击切换周月用电详情
	$scope.oWeek = function(){
		$scope.chart = true;
	};
	$scope.oMonth = function(){
		$scope.chart = false;
	};
	
	//开门记录openDoor
	var pagerBox_1 = document.getElementById('pager_3');
	var pagerBox_1_1 = document.getElementById('pager_3_1');
	$scope.openDoor = function(e,d){
		$scope.history = true;
		//data-dismiss="modal" data-toggle="modal" data-target="#modal_4_1"
		$('#modal_4').modal('hide')
		$('#modal_4').one('hidden.bs.modal', function (e) {
			 $('#modal_4_1').modal('show')
		})
		//周开门记录
		$scope.doorClear_1 = {};
		$scope.doorClear_1.devId = e;
		$scope.doorClear_1.numPerPage =5;
		$scope.doorClear_1.type = 'week';
		$scope.openDress = d;
		listSve.doorClear_1().list($scope.doorClear_1).then(function(e) {
			console.info(e)
			$scope.openlist_1 = e.res.data;
			//pager_3
			var count = Math.ceil(e.res.page.pageCount/5);
          	$scope.count_1 = count;
	          pagerBox_1.innerHTML="";
		 		 var pager = new Pager({
				index: 1,
				total: $scope.count_1,
				parent: pagerBox_1,
				onchange: doChangePage_1
			});
		})
		//月开门记录
		$scope.doorClear = {};
		$scope.doorClear.devId = e;
		$scope.doorClear.numPerPage =20;
		$scope.doorClear.type = 'month';
		$scope.openDress = d;
		listSve.doorClear_1().list($scope.doorClear).then(function(e) {
			console.info(e)
			$scope.openlist = e.res.data;
			//pager_3
			var count = Math.ceil(e.res.page.pageCount/20);
          	$scope.count_1_1 = count;
	          pagerBox_1_1.innerHTML="";
		 		 var pager = new Pager({
				index: 1,
				total: $scope.count_1_1,
				parent: pagerBox_1_1,
				onchange: doChangePage_1_1
			});
		})
		
		
	}
	//点击切换周月开门记录
	$scope.oWeek_1 = function(){
		$scope.history = true;
	};
	$scope.oMonth_1 = function(){
		$scope.history = false;
	};
	//分页器
	$scope.second = true;
		function doChangePage_1(obj) {
				if ($scope.second) {
				$scope.second = false;
			} else{
				console.info(obj.index)
				//,numPerPage:1
				$scope.pageInx = {pageNum:obj.index};
			 listSve.doorClear().list($scope.pageInx).then(function(e) {
			        	console.info('点击分页器返回数据'+e);
			            $scope.list = e.res.data;
			
					})
			 }
		}
	$scope.second_1 = true;
		function doChangePage_1_1(obj) {
				if ($scope.second_1) {
				$scope.second_1 = false;
			} else{
				console.info(obj.index)
				//,numPerPage:1
				$scope.pageInx = {pageNum:obj.index};
			 listSve.doorClear().list($scope.pageInx).then(function(e) {
			        	console.info('点击分页器返回数据'+e);
			            $scope.list = e.res.data;
			
					})
			 }
		}
	
}])